home *** CD-ROM | disk | FTP | other *** search
/ CD/PC Actual 31 / PC Actual CD 31.iso / dists / SRC / SLIBEXEC.AA / SLIBEXEC / libexec / rexecd / rexecd.c < prev   
Encoding:
C/C++ Source or Header  |  1997-11-26  |  7.8 KB  |  317 lines

  1. /*
  2.  * Copyright (c) 1983, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static const char copyright[] =
  36. "@(#) Copyright (c) 1983, 1993\n\
  37.     The Regents of the University of California.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. #if 0
  42. static char sccsid[] = "@(#)rexecd.c    8.1 (Berkeley) 6/4/93";
  43. #endif
  44. static const char rcsid[] =
  45.     "$Id: rexecd.c,v 1.15 1997/11/26 07:29:04 charnier Exp $";
  46. #endif /* not lint */
  47.  
  48. #include <sys/param.h>
  49. #include <sys/ioctl.h>
  50. #include <sys/socket.h>
  51. #include <sys/time.h>
  52.  
  53. #include <netinet/in.h>
  54. #include <arpa/inet.h>
  55.  
  56. #include <err.h>
  57. #include <netdb.h>
  58. #include <paths.h>
  59. #include <pwd.h>
  60. #include <signal.h>
  61. #include <stdio.h>
  62. #include <skey.h>
  63. #include <stdlib.h>
  64. #include <string.h>
  65. #include <syslog.h>
  66. #include <unistd.h>
  67.  
  68. char    username[MAXLOGNAME + 5 + 1] = "USER=";
  69. char    homedir[MAXPATHLEN + 5 + 1] = "HOME=";
  70. char    shell[MAXPATHLEN + 6 + 1] = "SHELL=";
  71. char    path[sizeof(_PATH_DEFPATH) + sizeof("PATH=")] = "PATH=";
  72. char    *envinit[] =
  73.         {homedir, shell, path, username, 0};
  74. char    **environ;
  75. char    *remote;
  76.  
  77. struct    sockaddr_in asin = { AF_INET };
  78.  
  79. void doit __P((int, struct sockaddr_in *));
  80. void getstr __P((char *, int, char *));
  81. /*VARARGS1*/
  82. void error __P(());
  83.  
  84. /*
  85.  * remote execute server:
  86.  *    username\0
  87.  *    password\0
  88.  *    command\0
  89.  *    data
  90.  */
  91. /*ARGSUSED*/
  92. int
  93. main(argc, argv)
  94.     int argc;
  95.     char **argv;
  96. {
  97.     struct sockaddr_in from;
  98.     int fromlen;
  99.     struct hostent *hp;
  100.  
  101.     openlog(argv[0], LOG_PID, LOG_AUTH);
  102.     fromlen = sizeof (from);
  103.     if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0)
  104.         err(1, "getpeername");
  105.  
  106.     hp = gethostbyaddr((char *) &from.sin_addr, sizeof(from.sin_addr),
  107.                from.sin_family);
  108.         remote = inet_ntoa(from.sin_addr);
  109.     remote = (hp != NULL) ? hp->h_name : inet_ntoa(from.sin_addr);
  110.  
  111.     doit(0, &from);
  112.     return(0);
  113. }
  114.  
  115. void
  116. doit(f, fromp)
  117.     int f;
  118.     struct sockaddr_in *fromp;
  119. {
  120.     FILE *fp;
  121.     char cmdbuf[NCARGS+1], *cp, *namep;
  122. #ifdef SKEY
  123.     char user[16], pass[100];
  124. #else /* SKEY */
  125.     char user[16], pass[16];
  126. #endif /* SKEY */
  127.     struct passwd *pwd;
  128.     int s;
  129.     u_short port;
  130.     int pv[2], pid, ready, readfrom, cc;
  131.     char buf[BUFSIZ], sig;
  132.     int one = 1;
  133.  
  134.     (void) signal(SIGINT, SIG_DFL);
  135.     (void) signal(SIGQUIT, SIG_DFL);
  136.     (void) signal(SIGTERM, SIG_DFL);
  137. #ifdef DEBUG
  138.     { int t = open(_PATH_TTY, 2);
  139.       if (t >= 0) {
  140.         ioctl(t, TIOCNOTTY, (char *)0);
  141.         (void) close(t);
  142.       }
  143.     }
  144. #endif
  145.     dup2(f, 0);
  146.     dup2(f, 1);
  147.     dup2(f, 2);
  148.     (void) alarm(60);
  149.     port = 0;
  150.     for (;;) {
  151.         char c;
  152.         if (read(f, &c, 1) != 1)
  153.             exit(1);
  154.         if (c == 0)
  155.             break;
  156.         port = port * 10 + c - '0';
  157.     }
  158.     (void) alarm(0);
  159.     if (port != 0) {
  160.         s = socket(AF_INET, SOCK_STREAM, 0);
  161.         if (s < 0)
  162.             exit(1);
  163.         if (bind(s, (struct sockaddr *)&asin, sizeof (asin)) < 0)
  164.             exit(1);
  165.         (void) alarm(60);
  166.         fromp->sin_port = htons(port);
  167.         if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0)
  168.             exit(1);
  169.         (void) alarm(0);
  170.     }
  171.     getstr(user, sizeof(user), "username");
  172.     getstr(pass, sizeof(pass), "password");
  173.     getstr(cmdbuf, sizeof(cmdbuf), "command");
  174.     setpwent();
  175.     pwd = getpwnam(user);
  176.     if (pwd == NULL) {
  177.         error("Login incorrect.\n");
  178.         exit(1);
  179.     }
  180.     endpwent();
  181.     if (*pwd->pw_passwd != '\0') {
  182. #ifdef SKEY
  183.         namep = skey_crypt(pass, pwd->pw_passwd, pwd,
  184.                    skeyaccess(user, NULL, remote, NULL));
  185. #else /* SKEY */
  186.         namep = crypt(pass, pwd->pw_passwd);
  187. #endif /* SKEY */
  188.         if (strcmp(namep, pwd->pw_passwd)) {
  189.             syslog(LOG_ERR, "LOGIN FAILURE from %s, %s",
  190.                    remote, user);
  191.             error("Login incorrect.\n");
  192.             exit(1);
  193.         }
  194.     }
  195.  
  196.     if (pwd->pw_uid == 0 || *pwd->pw_passwd == '\0' ||
  197.         (pwd->pw_expire && time(NULL) >= pwd->pw_expire)) {
  198.         syslog(LOG_ERR, "%s LOGIN REFUSED from %s", user, remote);
  199.         error("Login incorrect.\n");
  200.         exit(1);
  201.     }
  202.  
  203.     if ((fp = fopen(_PATH_FTPUSERS, "r")) != NULL) {
  204.         while (fgets(buf, sizeof(buf), fp) != NULL) {
  205.             if ((cp = index(buf, '\n')) != NULL)
  206.                 *cp = '\0';
  207.             if (strcmp(buf, pwd->pw_name) == 0) {
  208.                 syslog(LOG_ERR, "%s LOGIN REFUSED from %s",
  209.                        user, remote);
  210.                 error("Login incorrect.\n");
  211.                 exit(1);
  212.             }
  213.         }
  214.     }
  215.     (void) fclose(fp);
  216.  
  217.     syslog(LOG_INFO, "login from %s as %s", remote, user);
  218.  
  219.     (void) write(2, "\0", 1);
  220.     if (port) {
  221.         (void) pipe(pv);
  222.         pid = fork();
  223.         if (pid == -1)  {
  224.             error("Try again.\n");
  225.             exit(1);
  226.         }
  227.         if (pid) {
  228.             (void) close(0); (void) close(1); (void) close(2);
  229.             (void) close(f); (void) close(pv[1]);
  230.             readfrom = (1<<s) | (1<<pv[0]);
  231.             ioctl(pv[1], FIONBIO, (char *)&one);
  232.             /* should set s nbio! */
  233.             do {
  234.                 ready = readfrom;
  235.                 (void) select(16, (fd_set *)&ready,
  236.                     (fd_set *)NULL, (fd_set *)NULL,
  237.                     (struct timeval *)NULL);
  238.                 if (ready & (1<<s)) {
  239.                     if (read(s, &sig, 1) <= 0)
  240.                         readfrom &= ~(1<<s);
  241.                     else
  242.                         killpg(pid, sig);
  243.                 }
  244.                 if (ready & (1<<pv[0])) {
  245.                     cc = read(pv[0], buf, sizeof (buf));
  246.                     if (cc <= 0) {
  247.                         shutdown(s, 1+1);
  248.                         readfrom &= ~(1<<pv[0]);
  249.                     } else
  250.                         (void) write(s, buf, cc);
  251.                 }
  252.             } while (readfrom);
  253.             exit(0);
  254.         }
  255.         setpgrp(0, getpid());
  256.         (void) close(s); (void)close(pv[0]);
  257.         dup2(pv[1], 2);
  258.     }
  259.     if (*pwd->pw_shell == '\0')
  260.         pwd->pw_shell = _PATH_BSHELL;
  261.     if (f > 2)
  262.         (void) close(f);
  263.     if (setlogin(pwd->pw_name) < 0)
  264.         syslog(LOG_ERR, "setlogin() failed: %m");
  265.     (void) setgid((gid_t)pwd->pw_gid);
  266.     initgroups(pwd->pw_name, pwd->pw_gid);
  267.     (void) setuid((uid_t)pwd->pw_uid);
  268.     (void)strcat(path, _PATH_DEFPATH);
  269.     environ = envinit;
  270.     strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
  271.     strncat(shell, pwd->pw_shell, sizeof(shell)-7);
  272.     strncat(username, pwd->pw_name, sizeof(username)-6);
  273.     cp = strrchr(pwd->pw_shell, '/');
  274.     if (cp)
  275.         cp++;
  276.     else
  277.         cp = pwd->pw_shell;
  278.     if (chdir(pwd->pw_dir) < 0) {
  279.         error("No remote directory.\n");
  280.         exit(1);
  281.     }
  282.     execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
  283.     err(1, "%s", pwd->pw_shell);
  284. }
  285.  
  286. /*VARARGS1*/
  287. void
  288. error(fmt, a1, a2, a3)
  289.     char *fmt;
  290.     int a1, a2, a3;
  291. {
  292.     char buf[BUFSIZ];
  293.  
  294.     buf[0] = 1;
  295.     (void) snprintf(buf+1, sizeof(buf) - 1, fmt, a1, a2, a3);
  296.     (void) write(2, buf, strlen(buf));
  297. }
  298.  
  299. void
  300. getstr(buf, cnt, err)
  301.     char *buf;
  302.     int cnt;
  303.     char *err;
  304. {
  305.     char c;
  306.  
  307.     do {
  308.         if (read(0, &c, 1) != 1)
  309.             exit(1);
  310.         *buf++ = c;
  311.         if (--cnt == 0) {
  312.             error("%s too long\n", err);
  313.             exit(1);
  314.         }
  315.     } while (c != 0);
  316. }
  317.